package com.zdkj.subway.check.service.impl.subwayRat.cashBoxBatchAudit;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.subway.check.entity.CashBoxBatchAudit;
import com.zdkj.subway.check.entity.SwAuTvmCashboxAmountHierarchyDifference;
import com.zdkj.subway.check.entity.SwAuTvmMoneyAssist;
import com.zdkj.subway.check.mapper.CashBoxBatchAuditMapper;
import com.zdkj.subway.check.mapper.SwAuTvmCashboxAmountHierarchyDifferenceMapper;
import com.zdkj.subway.check.mapper.SwAuTvmMoneyAssistMapper;
import com.zdkj.subway.check.mapper.SysUserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 分钱箱审核
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class CashBoxBatchAuditServiceImpl {

    @Autowired
    private CashBoxBatchAuditMapper cashBoxBatchAuditMapper;
    @Autowired
    private SwAuTvmMoneyAssistMapper swAuTvmMoneyAssistMapper;
    @Autowired
    private SwAuTvmCashboxAmountHierarchyDifferenceMapper swAuTvmCashboxAmountHierarchyDifferenceMapper;
    @Resource
    private SysUserMapper sysUserMapper;

    /**
     * TVM票款差异分钱箱差异审核
     * @param page
     * @param param
     * @return
     */
    public IPage<CashBoxBatchAudit> getDifferencesInAudit(Page page, Map<String,Object> param) {
        /*Map<String,Object> p1=new HashMap<>();
        String lineCode = param.get("lineCode");
        String stationCode = param.get("stationCode");
        if(StringUtils.isNotEmpty(lineCode) || StringUtils.isNotEmpty(stationCode)){
            List<Map<String, Object>> maps = cashBoxBatchAuditMapper.getDifferencesInAudit(param);
            return maps;
        }
        p1.put("user_code",param.get("userCode"));
        List<SysUser> sysUsers = sysUserMapper.selectByMap(p1);
        SysUser sysUser = sysUsers.get(0);
        String managementLineStation = sysUser.getManagementLineStation();
        managementLineStation= StringUtils.isEmpty(managementLineStation)?"[]":managementLineStation;
        JSONArray managementLineStations = JSONArray.parseArray(managementLineStation);
        List<Map<String,Object>> cashBoxBatchAudits  =new ArrayList<>();
        for(int i=0;i<managementLineStations.size();i++){
            JSONObject jsonObject = managementLineStations.getJSONObject(i);
            String line = jsonObject.getString("line");
            String station = jsonObject.getString("station");
            param.put("lineCode",line);
            param.put("stationCode",station);
            List<Map<String,Object>> cashBoxBatchAuditsOne = cashBoxBatchAuditMapper.getDifferencesInAudit(param);
            cashBoxBatchAudits.addAll(cashBoxBatchAuditsOne);
        }*/
        IPage<CashBoxBatchAudit> maps = cashBoxBatchAuditMapper.getDifferencesInAudit(page,param);
        return maps;
    }


    /**
     * TVM分钱箱审核一键审核
     * @param param
     * @param user
     */
    public void updateAuditStatus(List param,PigxUser user) {	
        for(int i=0;i<param.size();i++){
            String ids = param.get(i)+"";
            //主要是防止硬币钱箱的id
            for(String id:ids.split(",")){
                Long o=Long.parseLong(id);
                CashBoxBatchAudit cashBoxBatchAudit = new CashBoxBatchAudit();
                cashBoxBatchAudit.setAuditStatus("1");
                cashBoxBatchAudit.setAuditTime(new Date());
				cashBoxBatchAudit.setAuditPersonCode(user.getUserCode());
                cashBoxBatchAudit.setAuditPersonName(user.getNikeName());
                cashBoxBatchAudit.setId(o);
                cashBoxBatchAuditMapper.updateAuditStatus(cashBoxBatchAudit);
            }
        }
    }

    /**
     * 分钱箱加入票款协查
     * @param param
     */
    public void insertSwAuTvmMoneyAssist(List param, String remark){
        for(int i=0;i<param.size();i++){
            String ids = param.get(i)+"";
            //主要是防止硬币钱箱的id
            for(String id:ids.split(",")) {
                Integer o = Integer.parseInt(id);
                CashBoxBatchAudit cashBoxBatchAudit = new CashBoxBatchAudit();
                cashBoxBatchAudit.setId(Long.valueOf(o));
                CashBoxBatchAudit differencesInAuditById = cashBoxBatchAuditMapper.getDifferencesInAuditById(cashBoxBatchAudit);
                if (null != differencesInAuditById.getIsAddAssist() && !"1".equals(differencesInAuditById.getIsAddAssist())) {
                    SwAuTvmMoneyAssist swAuTvmMoneyAssist = new SwAuTvmMoneyAssist();
                    swAuTvmMoneyAssist.setRelationid(o);
                    swAuTvmMoneyAssist.setLineCode(differencesInAuditById.getLineCode());
                    swAuTvmMoneyAssist.setStationCode(differencesInAuditById.getStationCode());
                    SimpleDateFormat dff = new SimpleDateFormat("yyyy-MM-dd");
                    swAuTvmMoneyAssist.setSettleTime(dff.format(differencesInAuditById.getRecordTime()));
                    swAuTvmMoneyAssist.setTvmOrder(differencesInAuditById.getTvmCode());
                    swAuTvmMoneyAssist.setTvmUnloadMoney(differencesInAuditById.getUnloadDisplayMoney());
                    swAuTvmMoneyAssist.setActualMoney(differencesInAuditById.getActualMoney());
                    swAuTvmMoneyAssist.setHandOutMoney(differencesInAuditById.getHandOutMoney());
                    swAuTvmMoneyAssist.setSupplementMoney(differencesInAuditById.getSupplementMoney());
                    swAuTvmMoneyAssist.setSystemMoney(differencesInAuditById.getSystemMoney());
                    swAuTvmMoneyAssist.setDiffMoney(differencesInAuditById.getDiffMoney());
                    swAuTvmMoneyAssist.setBoxCashType(differencesInAuditById.getCashBoxType());
                    swAuTvmMoneyAssist.setAssistInvestStatus("1");
                    swAuTvmMoneyAssist.setKyAssistInvestStatus("1");
                    swAuTvmMoneyAssist.setReplyStatus("0");
                    swAuTvmMoneyAssist.setReplaceStatus("0");
                    swAuTvmMoneyAssist.setCreatorId(differencesInAuditById.getCreatorId());
                    swAuTvmMoneyAssist.setState("1");
                    swAuTvmMoneyAssist.setStationCleanRemark(remark);
                    swAuTvmMoneyAssistMapper.insertSwAuTvmMoneyAssist(swAuTvmMoneyAssist);
                    differencesInAuditById.setIsAddAssist("1");
                    cashBoxBatchAuditMapper.updateAuditStatus(differencesInAuditById);
                }
            }
        }
    }
    //分钱箱加入层级协查
    public void insertSwAuTvmCashboxAmountHierarchyDifference(List param){
        for(int i=0;i<param.size();i++){
            String ids = param.get(i)+"";
            //主要是防止硬币钱箱的id
            for(String id:ids.split(",")) {
                Integer o = Integer.parseInt(id);
                CashBoxBatchAudit cashBoxBatchAudit = new CashBoxBatchAudit();
                cashBoxBatchAudit.setId(Long.valueOf(o));
                CashBoxBatchAudit differencesInAuditById = cashBoxBatchAuditMapper.getDifferencesInAuditById(cashBoxBatchAudit);
                if (null != differencesInAuditById.getIsAddLevelAssist() && !"1".equals(differencesInAuditById.getIsAddLevelAssist())) {
                    SwAuTvmCashboxAmountHierarchyDifference swAuTvmCashboxAmountHierarchyDifference = new SwAuTvmCashboxAmountHierarchyDifference();
                    swAuTvmCashboxAmountHierarchyDifference.setRelationSourceboxId(o);
                    swAuTvmCashboxAmountHierarchyDifference.setLineCode(differencesInAuditById.getLineCode());
                    swAuTvmCashboxAmountHierarchyDifference.setStationCode(differencesInAuditById.getStationCode());
                    //结算日期
                    swAuTvmCashboxAmountHierarchyDifference.setSettleTime(differencesInAuditById.getRecordTime());
                    swAuTvmCashboxAmountHierarchyDifference.setTvmOrder(differencesInAuditById.getTvmCode());
                    swAuTvmCashboxAmountHierarchyDifference.setTvmMoneyOrder(differencesInAuditById.getCashBoxCode());
                    swAuTvmCashboxAmountHierarchyDifference.setTvmMoneyType(differencesInAuditById.getCashBoxType());
//                swAuTvmCashboxAmountHierarchyDifference.setReplyStatus("0");
                    swAuTvmCashboxAmountHierarchyDifference.setType("2");
                    swAuTvmCashboxAmountHierarchyDifference.setDepositAmount(differencesInAuditById.getBillOrderMoney());
                    swAuTvmCashboxAmountHierarchyDifference.setSaleAmount(differencesInAuditById.getScSaleAmount());
                    swAuTvmCashboxAmountHierarchyDifference.setAssistStatus("1");
                    swAuTvmCashboxAmountHierarchyDifference.setReplaceStatus("0");
                    swAuTvmCashboxAmountHierarchyDifference.setCreatorId(differencesInAuditById.getCreatorId());
                    swAuTvmCashboxAmountHierarchyDifferenceMapper.insertSwAuTvmCashboxAmountHierarchyDifference(swAuTvmCashboxAmountHierarchyDifference);
                    differencesInAuditById.setIsAddLevelAssist("1");
                    cashBoxBatchAuditMapper.updateAuditStatus(differencesInAuditById);
                }
            }
        }
    }
}
